
package results;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Used to manipulate snapshots (FOR TEST PURPOSES ONLY).<br />
 * IT HAS BEEN REPLACED BY OBJECT READER AND OBJECT WRITER in Utils Fm
 * 
 * @author Guillaume
 */
public class SnapshotFactory {

	private static final Logger fLogger = Logger.getLogger(SnapshotFactory.class.getPackage().getName());

	public List quarks = new ArrayList();
	
	public SnapshotFactory(){
		/*quarks.add("up");
		quarks.add("down");
		quarks.add("strange");
		quarks.add("charm");
		quarks.add("top");
		quarks.add("bottom");*/
		
		// to make this working
		// you have to create a probe
		
		/*
		quarks.add(new ProbeCom());
		quarks.add(new ProbeCom());
		quarks.add(new ProbeCom());
		quarks.add(new ProbeCom());
		quarks.add(new ProbeCom());
		quarks.add(new ProbeCom());
		*/
	}
	
	public void removeQuark(){
		quarks.clear();
	}
	
	public void print(){
		Iterator quarksItr = quarks.iterator();
		 while ( quarksItr.hasNext() ) {
		   System.out.println( ((Probe)quarksItr.next()).toString() );
		 }
	}

	public void writeObject(){
		ObjectOutput output = null;
		try{
		  //use buffering
		  OutputStream file = new FileOutputStream("quarks3.ser",true);
		  OutputStream buffer = new BufferedOutputStream( file );
		  output = new ObjectOutputStream( buffer );
		  output.writeObject( quarks );
		}
		catch(IOException ex){
		  fLogger.log(Level.SEVERE, "Cannot perform output.", ex);
		}
		finally{
		  try {
			if (output != null) {
			  //flush and close "output" and its underlying streams
			  output.close();
			}
		  }
		  catch (IOException ex ){
			fLogger.log(Level.SEVERE, "Cannot close output stream.", ex);
		  }
		}
	}
	
	public void readObject(){
		//deserialize the quarks.ser file
		//note the use of generic interface references

		//declared here only to ensure visibilty in finally clause
		ObjectInput input = null;
		try{
		  //use buffering
		  InputStream file = new FileInputStream( "quarks3.ser" );
		  InputStream buffer = new BufferedInputStream( file );
		  input = new ObjectInputStream ( buffer );
		  //deserialize the List
		  quarks = (ArrayList)input.readObject();
		  //display its data
		  Iterator quarksItr = quarks.iterator();
		  /* while ( quarksItr.hasNext() ) {
			System.out.println( (String)quarksItr.next() );
		  }*/
		}
		catch(IOException ex){
		  fLogger.log(Level.SEVERE, "Cannot perform input.", ex);
		}
		catch (ClassNotFoundException ex){
		  fLogger.log(Level.SEVERE, "Unexpected class found upon input.", ex);
		}
		finally{
		  try {
			if ( input != null ) {
			  //close "input" and its underlying streams
			  input.close();
			}
		  }
		  catch (IOException ex){
			fLogger.log(Level.SEVERE, "Cannot close input stream.", ex);
		  }
		}
	  }

	public static void main(String[] args) {
		SnapshotFactory f = new SnapshotFactory();
		System.out.println( "coucou");
		f.writeObject();
		f.removeQuark();
		System.out.println( "coucou");
		f.readObject();
		System.out.println( "coucou");
		f.print();
		
	}
}
